
	set more off
	
	global data ""
	
	clear all
	use "$data/choice_data.dta", clear	

	*********************************************************************
	***					WILLINGESS to TAKE RISKS 				      ***
	*********************************************************************

	*** STANDARD DEVIATION of SELECTED OPTION (THIS IS MAIN MEASURE)
	
	gen SD1 = .
		replace SD1 = 0 			if risk_G_J == 1  
		replace SD1 = (15-0)		if risk_G_J == 4
		replace SD1 = 0.5*(15-0)	if risk_G_J == 15

	gen SD2 = .
		replace SD2 = 0				if risk_G_L == 1  
		replace SD2 = (9 -0)		if risk_G_L == 6
		replace SD2 = 0.5*(9 -0)	if risk_G_L == 15
		
	gen SD3 = .
		replace SD3 = 0			 	if risk_H_J == 2	
		replace SD3 = (15-0)		if risk_H_J == 4
		replace SD3 = 0.5*(15-0)	if risk_H_J == 15

	gen SD4 = .
		replace SD4 = 0			 	if risk_H_K == 2  	
		replace SD4 = (12-3)		if risk_H_K == 5
		replace SD4 = 0.5*(12-3)	if risk_H_K == 15
		
	gen SD5 = .
		replace SD5 = 0				if risk_H_L == 2  	
		replace SD5 = (9 -0)		if risk_H_L == 6
		replace SD5 = 0.5*(9 -0)	if risk_H_L == 15
		
	gen SD6 = .
		replace SD6 = 0				if risk_I_J == 3  	
		replace SD6 = (15-0)		if risk_I_J == 4
		replace SD6 = 0.5*(15-0)	if risk_I_J == 15

	gen SD7 = .
		replace SD7 = 0				if risk_I_K == 3  	
		replace SD7 = (12-3)		if risk_I_K == 5
		replace SD7 = 0.5*(12-3)	if risk_I_K == 15
		
	egen risk_aversion = rowmean(SD1 SD2 SD3 SD4 SD5 SD6 SD7)
	
	*********************************************************************
	***			AVERSION to DISADVANTAGEOUS INEQUALITY   			  ***
	*********************************************************************
	
	gen aversion_DI_1 = .
		replace aversion_DI_1 = (3-3)			if sharing_Q_R == 3
		replace aversion_DI_1 = (3-6)			if sharing_Q_R == 4
		replace aversion_DI_1 = 0.5*(3-6)		if sharing_Q_R == 10
		
	gen aversion_DI_2 = .
		replace aversion_DI_2 = (3-3)			if sharing_Q_S == 3
		replace aversion_DI_2 = (4-6)			if sharing_Q_S == 5	
		replace aversion_DI_2 = 0.5*(4-6)		if sharing_Q_S == 10	
		
	egen aversion_DI = rowmean(aversion_DI_1 aversion_DI_2)	
				
	*********************************************************************
	***			PREFERENCE for ADVANTAGEOUS INEQUALITY   			  ***
	*********************************************************************

	gen pref_AI_1 = .
		replace pref_AI_1 = (3-1)				if sharing_P_Q == 2
		replace pref_AI_1 = (3-3)				if sharing_P_Q == 3				
		replace pref_AI_1 = 0.5*(3-1)			if sharing_P_Q == 10				

	gen pref_AI_2= .
		replace pref_AI_2 = (2-0)				if sharing_O_Q == 1
		replace pref_AI_2 = (3-3)				if sharing_O_Q == 3				
		replace pref_AI_2 = 0.5*(2-0)			if sharing_O_Q == 0				
				
	egen pref_AI     = rowmean(pref_AI_1 pref_AI_2)		
	
	******************************************************
	***	PROGRAM to IDENTIFY VIOLATIONS of TRANSITIVITY ***
	******************************************************

	* THIS PROGRAM CREATES A SINGLE MEASURE, THE TOTAL NUMBER VIOLATIONS across ALL TRIOS
	
	global weight 1 // weight assigned to trio including at least one indifference
	
	capture program drop violations
	program define violations
	
		
		local m = 1
		foreach trio in `2' {
			
			local option1 = substr("`trio'",1,1)			
			local option2 = substr("`trio'",3,1)
			local option3 = substr("`trio'",5,1)
			
		
			gen `1'`m' = 0

		
			// no indifferences
			replace `1'`m' = 1			if (choice_`option1'_`option2' == "`option1'" 		& choice_`option2'_`option3' == "`option2'"			& choice_`option1'_`option3' == "`option3'") 
			replace `1'`m' = 1			if (choice_`option1'_`option2' == "`option2'" 		& choice_`option2'_`option3' == "`option3'"			& choice_`option1'_`option3' == "`option1'") 
			
			
			// one indifference
			replace `1'`m' = $weight  	if (choice_`option1'_`option2' == "Indifferent" 	& choice_`option2'_`option3' == "`option3'"			& choice_`option1'_`option3' == "`option1'") 
			replace `1'`m' = $weight  	if (choice_`option1'_`option2' == "Indifferent" 	& choice_`option2'_`option3' == "`option2'"			& choice_`option1'_`option3' == "`option3'") 
			
			replace `1'`m' = $weight	if (choice_`option1'_`option2' == "`option1'" 		& choice_`option2'_`option3' == "`option2'"			& choice_`option1'_`option3' == "Indifferent") 
			replace `1'`m' = $weight	if (choice_`option1'_`option2' == "`option2'" 		& choice_`option2'_`option3' == "`option3'"			& choice_`option1'_`option3' == "Indifferent") 
		
			replace `1'`m' = $weight	if (choice_`option1'_`option2' == "`option1'" 		& choice_`option2'_`option3' == "Indifferent"		& choice_`option1'_`option3' == "`option3'") 
			replace `1'`m' = $weight	if (choice_`option1'_`option2' == "`option2'" 		& choice_`option2'_`option3' == "Indifferent"		& choice_`option1'_`option3' == "`option1'") 

			
			// two indifferences
			replace `1'`m' = $weight  	if (choice_`option1'_`option2' == "Indifferent" 	& choice_`option2'_`option3' == "Indifferent"		& choice_`option1'_`option3' == "`option1'") 
			replace `1'`m' = $weight  	if (choice_`option1'_`option2' == "Indifferent" 	& choice_`option2'_`option3' == "Indifferent"		& choice_`option1'_`option3' == "`option3'") 
			
			replace `1'`m' = $weight	if (choice_`option1'_`option2' == "Indifferent" 	& choice_`option2'_`option3' == "`option2'"			& choice_`option1'_`option3' == "Indifferent") 
			replace `1'`m' = $weight	if (choice_`option1'_`option2' == "Indifferent" 	& choice_`option2'_`option3' == "`option3'"			& choice_`option1'_`option3' == "Indifferent") 
		
			replace `1'`m' = $weight	if (choice_`option1'_`option2' == "`option1'" 		& choice_`option2'_`option3' == "Indifferent"		& choice_`option1'_`option3' == "Indifferent") 
			replace `1'`m' = $weight	if (choice_`option1'_`option2' == "`option2'" 		& choice_`option2'_`option3' == "Indifferent"		& choice_`option1'_`option3' == "Indifferent") 
			
			local m = `m' + 1

		}			
			
	end
	*/	
	
	******************************************************
	***				CONSISTENCY IN TOY TASK			   ***
	******************************************************
	
	label drop toys
	label define toys 	///
	1 "A" 			///
	2 "B" 		///
	3 "C" 		///
	4 "D" 			///
	5 "E" 		///
	6 "F" 			///
	7 "Indifferent"	

	// using labels to transform from numeric to string variable
	foreach choice in 	A_B A_C A_D A_E A_F B_C B_D B_E B_F C_D C_E C_F D_E D_F E_F {
		decode toy_`choice', gen(choice_`choice')
	}
	
	// creating a list with all possible toy trios
	
	local toy_options "A B C D E F"

	local toy_trios ""
	
	forvalues i = 1/4 {
		
		local option1 : word `i' of `toy_options'
		
		local j = `i' + 1
		
		forvalues k = `j'/5 {
			
			local option2 : word `k' of `toy_options'
			
			local l = `k' + 1
			
			forvalues m = `l'/6 {
				
				local option3 : word `m' of `toy_options'
				
				local toy_trios "`toy_trios' `option1'_`option2'_`option3'"

			}
			
		}
		
	}		
	
	cap drop toys_transitivit*
	
	violations toys_transitivity "`toy_trios'"
	
	egen toys_transitivity = rowtotal(toys_transitivity*)
	
	******************************************************
	***			CONSISTENCY IN SHARING TASK			   ***
	******************************************************
	
	label drop sharing
	label define sharing 	///
	1 "O" 		///
	2 "P" 		///
	3 "Q" 		///
	4 "R" 		///
	5 "S" 		///
	10 "Indifferent"
	
	// using labels to transform from numeric to string variable
	foreach choice in 	O_P O_Q O_R O_S P_Q P_R P_S Q_R Q_S R_S {
		decode sharing_`choice', gen(choice_`choice')
	}
	
	
	// creating a list with all possible sharing trios
	local sharing_options "O P Q R S"

	local sharing_trios ""
	
	forvalues i = 1/3 {
		
		local option1 : word `i' of `sharing_options'
		
		local j = `i' + 1
		
		forvalues k = `j'/4 {
			
			local option2 : word `k' of `sharing_options'
			
			local l = `k' + 1
			
			forvalues m = `l'/5 {
				
				local option3 : word `m' of `sharing_options'
				
				local sharing_trios "`sharing_trios' `option1'_`option2'_`option3'"

			}
			
		}
		
	}		
	
	cap drop sharing_transitivit*
	
	violations sharing_transitivity "`sharing_trios'"
	
	egen sharing_transitivity = rowtotal(sharing_transitivity*)
	
	******************************************************
	***			CONSISTENCY IN RISK TASK			   ***
	******************************************************
	
	cap drop risk_consistenc*
	
	* (0,9) chosen over (3,3) and (3,3) chosen over (0,15)
	
	gen risk_consistency1 = 0			if	 risk_G_L < .	& risk_G_J < . 
		replace risk_consistency1 = 1	if 	(risk_G_L == 6 	| risk_G_L == 15) & (risk_G_J == 1 	| risk_G_J == 15)
		

	* (3,3) chosen over (0,9) and (0,9) chosen over (6,6)
	
	gen risk_consistency2 = 0			if	 risk_G_L < .	& risk_H_L < . 
		replace risk_consistency2 = 1	if 	(risk_G_L == 1 	| risk_G_L == 15) & (risk_H_L == 6 	| risk_H_L == 15)

		
	* (0,9) chosen over (6,6) and (6,6) chosen over (0,15) 
			
	gen risk_consistency3 = 0			if	 risk_H_L <  .	& risk_H_J < . 	 
		replace risk_consistency3 = 1	if 	(risk_H_L == 6 	| risk_H_L == 15) & (risk_H_J == 2 	| risk_H_J == 15)

		
	* (3,3) chosen over (0,15) and (0,15) chosen over (6,6) 
			
	gen risk_consistency4 = 0			if	 risk_G_J <  .	& risk_H_J < . 	 
		replace risk_consistency4 = 1	if 	(risk_G_J == 1 	| risk_G_J == 15) & (risk_H_J == 4 	| risk_H_J == 15)
		
			
	* (3,3) chosen over (0,15) and (0,15) chosen over (9,9)
			
	gen risk_consistency5 = 0			if	 risk_G_J <  .	& risk_I_J < . 	 
		replace risk_consistency5 = 1	if 	(risk_G_J == 1 	| risk_G_J == 15) & (risk_I_J == 4 	| risk_I_J == 15) 
		

	* (6,6) chosen over (0,15) and (0,15) chosen over (9,9)
			
	gen risk_consistency6 = 0			if	 risk_H_J <  .	& risk_I_J < . 	 
		replace risk_consistency6 = 1	if 	(risk_H_J == 2 	| risk_H_J == 15) & (risk_I_J == 4 	| risk_I_J == 15)

		
	* (0,9) chosen over (6,6) and (6,6) chosen over (3,12)
			
	gen risk_consistency7 = 0			if	 risk_H_L <  .	& risk_H_K < . 	 
		replace risk_consistency7 = 1	if 	(risk_H_L == 6 	| risk_H_L == 15) & (risk_H_K ~= 5  & risk_H_K < .)


	* (3,12) chosen over (9,9) and (6,6) chosen over (3,12)
			
	gen risk_consistency8 = 0			if	 risk_I_K <  .	& risk_H_K < . 	 
		replace risk_consistency8 = 1	if 	(risk_I_K == 5 	| risk_I_K == 15) & (risk_H_K ~= 5  & risk_H_K < .)

		
	gen risk_consistency = risk_consistency1 + risk_consistency2 + risk_consistency3 + risk_consistency4 + risk_consistency5 + risk_consistency6 + risk_consistency7 + risk_consistency8       
		
	******************************************************
	***		DOMINATED CHOICES IN RISK TASK			   ***
	******************************************************
	 
	*** (3,12) dominates (3,3)	
	gen risk_dominated1 = 0	if risk_G_K == 5
		replace risk_dominated1 = 1	if risk_G_K == 1 | risk_G_K == 15 
		
	*** (3,6) dominates (3,3)	
	gen risk_dominated2 = 0	if risk_G_M == 7
		replace risk_dominated2 = 1	if risk_G_M == 1 | risk_G_M == 15 
		
	*** (3,3) dominates (0,3)	
	gen risk_dominated3 = 0	if risk_G_N == 1
		replace risk_dominated3 = 1	if risk_G_N == 8 | risk_G_N == 15 
		
	*** (6,6) dominates (3,6)	
	gen risk_dominated4 = 0	if risk_H_M == 2
		replace risk_dominated4 = 1	if risk_H_M == 7 | risk_H_M == 15 
		
	*** (6,6) dominates (0,3)	
	gen risk_dominated5 = 0	if risk_H_N == 2
		replace risk_dominated5 = 1	if risk_H_N == 8 | risk_H_N == 15 
		
	*** (9,9) dominates (0,9)	
	gen risk_dominated6 = 0	if risk_I_L == 3
		replace risk_dominated6 = 1	if risk_I_L == 6 | risk_I_L == 15 
		
	*** (9,9) dominates (3,6)	
	gen risk_dominated7 = 0	if risk_I_M == 3
		replace risk_dominated7 = 1	if risk_I_M == 7 | risk_I_M == 15 
		
	*** (9,9) dominates (0,3)	
	gen risk_dominated8 = 0	if risk_I_N == 3
		replace risk_dominated8 = 1	if risk_I_N == 8 | risk_I_N == 15
		
	
	egen risk_dominated = rowtotal(risk_dominated*)
	
	******************************************************
	***			DECISION-MAKING QUALITY INDEX   	   ***
	******************************************************
	
	gsem (toys_transitivity sharing_transitivity risk_consistency risk_dominated <- X)

	predict DMQ, latent	
	
	replace DMQ = -DMQ

	
	
	
	preserve
	
		keep ID num_quest risk_dominated* risk_consistency* toys_transitivity* sharing_transitivity*
	
		drop risk_dominated risk_consistency toys_transitivity sharing_transitivity
		
		
		sort ID
		save "$data/secondary/disaggregated_mistakes.dta", replace
		
	restore 
	
	
	
	keep ID num_quest risk_aversion aversion_DI pref_AI waiting_time toys_transitivity sharing_transitivity risk_consistency risk_dominated DMQ // fair_DItrial fair_AItrial 
	
	label variable risk_aversion "Willingness to take risks"
	label variable aversion_DI "Aversion to disadvantageous inequality"
	label variable pref_AI "Preference for advantageous inequality"
	label variable waiting_time "Waiting time marshmallow task"
	label variable toys_transitivity "Violations transitivy toys task"
	label variable sharing_transitivity "Violations transitivy sharing task"
	label variable risk_consistency "Violations transitivity/dominance risk task"
	label variable risk_dominated "Dominated choices risk task"
	label variable DMQ "Decision-Making Quality Index"
	
	order waiting_time, after(pref_AI)
	
	order DMQ toys_transitivity sharing_transitivity risk_consistency risk_dominated, before(risk_aversion)
	
	
	
	
	
	merge 1:1 ID using "$data/other_data.dta", nogen keep(match)
	
	order male ITT cluster daycare_attendance, before(DMQ) 

	
	
	gen male_ITT = male * ITT
	gen female_ITT = (1-male) * ITT
	
	
	label variable male_ITT "Male x ITT"
	label variable female_ITT "Female x ITT"
	
	order male_ITT female_ITT, after(ITT)
	
	
	
		
	foreach var in  DMQ risk_aversion aversion_DI pref_AI waiting_time {
	
		sum `var' if ITT == 0
		ge m`var' = r(mean)
		ge s`var' = r(sd)
		ge st_`var' = (`var' - m`var')/s`var'
		
	}
	*/
	
	label variable st_risk_aversion "Standardized risk_aversion"
	label variable st_aversion_DI "Standardized aversion_DI"
	label variable st_pref_AI "Standardized pref_AI"
	label variable st_waiting_time "Standardized waiting_time"
	label variable st_DMQ "Standardized DMQ"
	
	foreach var in DMQ risk_aversion aversion_DI pref_AI waiting_time {
	
	order st_`var', after(`var')
	
	}
	
		
	save "$data/secondary/final_data.dta", replace
